package com.jinzecloud.fiction.service.impl;

import com.jinzecloud.fiction.bean.TbBookName;
import com.jinzecloud.fiction.mapper.TbBookNameMapper;
import com.jinzecloud.fiction.service.TbBookNameService;
import com.jinzecloud.fiction.vo.TbCatalogueVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinzecloud.fiction.utils.PageUtils;
import com.jinzecloud.fiction.utils.Query;

import com.jinzecloud.fiction.mapper.TbCatalogueMapper;
import com.jinzecloud.fiction.bean.TbCatalogue;
import com.jinzecloud.fiction.service.TbCatalogueService;


@Service("tbCatalogueService")
public class TbCatalogueServiceImpl extends ServiceImpl<TbCatalogueMapper, TbCatalogue> implements TbCatalogueService {

    @Autowired
    private TbBookNameService tbBookNameService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<TbCatalogue> tbCatalogueQueryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            tbCatalogueQueryWrapper.eq("cat_id",key);
        }
        if(!StringUtils.isEmpty(key)){
            tbCatalogueQueryWrapper.or().like("cat_catalogue_name",key);
        }
        IPage<TbCatalogue> page = this.page(
                new Query<TbCatalogue>().getPage(params),
                tbCatalogueQueryWrapper
        );
        PageUtils pageUtils = new PageUtils(page);
        List<TbCatalogue> records = page.getRecords();
        List<TbCatalogueVo> list = records.stream().map(tbCatalogue -> {
            TbCatalogueVo tbCatalogueVo = new TbCatalogueVo();
            TbBookName tbBookNameVo = tbBookNameService.getById(tbCatalogue.getCatBookNameId());
            tbCatalogueVo.setBnName(tbBookNameVo.getBnName());
            BeanUtils.copyProperties(tbCatalogue, tbCatalogueVo);
            return tbCatalogueVo;
        }).collect(Collectors.toList());
        pageUtils.setList(list);
        return pageUtils;
    }

}